﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class Forum : System.Web.UI.Page
{

    private CForum m_forum;
    private int m_curpage;
    private int m_topicnum;
    private int m_num_per_page=10;
    private int m_fid;
    private int m_probid=-1;
    private bool isProblemForum=false;

    //创建主题标题，0 普通版块，1题目版块
    string CreatTopic(Topic t,int type=0)   
    {
        if (t.status == 0)
            t.title += "(已删除)";

       
        string prob_link;
        string R = "";
        string link = "ViewTopic.aspx?tid=" + t.tid.ToString() + "&page=1";
        string userlink = "personal.aspx?uid="+t.m.uid;
        string title = Server.HtmlEncode(t.title);
        string author = t.m.nickname;
        string post_time = t.posttime.ToString();
        string last_reply_time = t.lastreptime.ToString();
        string viewnum = t.viewnum.ToString();
        string replynum = t.replynum.ToString();


        R += "<tr><td>";

        //设置题目链接
        if (type == 1)
        {
            prob_link = "<a href=\"http://icpc.ahu.edu.cn/OJ/Problem.aspx?id="+t.ProbID+"\">[ " + t.ProbID + " ]</a>";
            R+=prob_link;
        }

        R += "&nbsp;&nbsp;<a href=\"" + link + "\">" + title + "</a>";
        R += "</td><td><a href=\"" + userlink + "\">" + author + "</a>";
        R += "<p>" + post_time + "</p></td><td>" + viewnum + "</td>";
        R += "<td>" + replynum + "</td>";
        R += "<td>" + last_reply_time + "</td></tr>";

        return R;
    }

   


    protected void Page_Load(object sender, EventArgs e)
    {
        MemDisplay m = new MemDisplay(1);
        m.GetOnlineTime();

        CGeneral.CheckLogin(Request, Session);
        forum_problem_wrap.Visible = false;


        if (Request.QueryString["fid"] == null)
        {
            //提示 无效的版块id
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "未指定版块");
        }
        if (Request.QueryString["page"] == null)
        {
            //提示 无效的版块id
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "未指定页码");
        }
        if (Request.QueryString["probid"] != null)
        {
            m_probid = Check.CheckToInt(Request.QueryString["probid"].ToString());
            if(m_probid==-1)
                CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "无效的ProbID");
        }

        //权限管理未设置
        Permission per = new Permission(int.Parse(Session["gid"].ToString()));


        //初始化成员变量
        m_fid = Check.CheckToInt(Request.QueryString["fid"]);

        if (m_fid == -1)
        {
            //提示 无效的版块id
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "无效的fid");
        }
        //判断是否是题目版块
        if (m_fid == int.Parse(System.Configuration.ConfigurationManager.AppSettings["ProbForumId"]))
        {
            isProblemForum = true;
        }
        //判断是否为回收站
        if(m_fid==int.Parse(System.Configuration.ConfigurationManager.AppSettings["Recycling"]))
        {
            if(per.awmag==0)
                CGeneral.GoToErrorPage(Response, "custom", "YOU NEED MORE POWER");

        }
        m_forum=new CForum(m_fid);
        if(m_forum.isExist==0)
        {
            //提示 不存在此版块
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "不存在此版块");
        }

        m_curpage = Check.CheckToInt(Request.QueryString["page"]);
        if(m_curpage == -1)
        {
            //提示 无效的页码id
            CGeneral.GoToErrorPage(Response, "custom", "缺少参数", "无效的page");
        }



        //获取版块的描述信息
        m_forum.GetDescription();
        Literaldescription.Text = m_forum.m_description;


        //此处产生每个主题的标题
        if (isProblemForum && m_probid!=-1 )             //是题目版块
        {
            //(m_probid==-1)
            if (per.awmag == 1)
            {
                m_forum.SearchAojProblem(m_probid, m_num_per_page, m_curpage, 1);
            }
            else
            {
                m_forum.SearchAojProblem(m_probid, m_num_per_page, m_curpage, 0);
            }
        }
        else                            //普通版块
        {       
            if (per.awmag == 1)
            {
                m_forum.getTopicCount(1);
                m_forum.getTopic(m_num_per_page, m_curpage, 1);
            }
            else
            {
                m_forum.getTopicCount(0);
                m_forum.getTopic(m_num_per_page, m_curpage, 0);
            }
        }
        //如果页码超出最大页码

        int maxPage = m_forum.topicCount / m_num_per_page;
        if (m_forum.topicCount % m_num_per_page != 0)
            maxPage++;
        if (m_curpage > maxPage)
            m_curpage = maxPage;
       
        foreach (Topic t in m_forum.topics)
        {
            this.Literal_topic.Text += CreatTopic(t,isProblemForum?1:0);
        }

        //初始化部分页面信息
        new_topic_button.NavigateUrl = "CreateThread.aspx?fid=" + Request.QueryString["fid"];

        //初始化page index tab
        PageTab1.curpage = m_curpage;
        PageTab1.topicnum = m_forum.topicCount;
        //特殊处理问题版块页符
        if (isProblemForum && m_probid != -1)
            PageTab1.url = "forum.aspx?fid=" + m_fid +"&probid="+m_probid+ "&page=";
        else 
            PageTab1.url = "forum.aspx?fid="+m_fid+"&page=";
        PageTab1.num_per_page=m_num_per_page;

        PageTab2.curpage = m_curpage;
        PageTab2.topicnum = m_forum.topicCount;
        //特殊处理问题版块页符
        if (isProblemForum && m_probid != -1)
            PageTab1.url = "forum.aspx?fid=" + m_fid + "&probid=" + m_probid + "&page=";
        else 
            PageTab2.url = "forum.aspx?fid=" + m_fid + "&page=";
        PageTab2.num_per_page = m_num_per_page;

        //初始化导航

        List<string> urls = new List<string>();
        urls.Add("Index.aspx");
        //特殊处理问题版块导航
        if(isProblemForum && m_probid!=-1)
            urls.Add("Forum.aspx?fid=" + m_fid + "&page=1&probid="+m_probid);
        else
            urls.Add("Forum.aspx?fid=" + m_fid + "&page=1");
        navigation.urls = urls;
        List<string> names = new List<string>();
        names.Add("首页");
        names.Add(m_forum.m_name);
        navigation.names = names;

        //初始化搜索题目

        if(isProblemForum)
        {
            forum_problem_wrap.Visible = true;
            problem_btn.HRef = "Forum.aspx?fid=" + m_fid + "&page=1";
        }
        //设置 显示全部 链接
        show_all.HRef = "Forum.aspx?fid=" + m_fid + "&page=1";

    }
}